#!/bin/sh
echo -e "\nValidating Javascript:\n"
# STAGED_FILES=$(git status -s | grep "[M|A|??].*js$" | awk '{printf $2 " "}')
STAGED_FILES=$(find ./ -name *.js)

if [[ "$STAGED_FILES" = "" ]]; then
    exit 0
fi

# Check for eslint
which eslint &> /dev/null
if [[ "$?" == 1 ]]; then
  echo -e "\t\033[41mPlease install ESlint\033[0m"
  exit 1
fi

eslint $STAGED_FILES
if [[ "$?" == 1 ]]; then
  echo -e "\033[41mCOMMIT FAILED:\033[0m Your commit contains files that should pass ESLint but do not. Please fix the ESLint errors and try again.\n"
  exit 1
else
  echo -e "\033[42mCOMMIT SUCCEEDED\033[0m\n"
  exit 0
fi